<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="handleAddCateDialogVisible">添加分类</el-button>
        </el-col>
      </el-row>
      <TableTree
        class="TableTree"
        :data="TableTreeData"
        :columns="TableTreeColumns"
        show-index
        :selection-type="false"
        :expand-type="false"
        index-text="#"
        border
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:green" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="danger" v-else>三级</el-tag>
        </template>
        <template slot="action" slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEditCateDialogVisible(scope.row.cat_id)"
          >编辑</el-button>

          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleRemoveCate(scope.row.cat_id)"
          >删除</el-button>
        </template>
      </TableTree>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[1, 3, 5]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <el-dialog
        @close="handleAddCateDialogClose"
        title="添加分类"
        :visible.sync="AddCateDialogVisible"
        width="50%"
      >
        <el-form
          :model="AddCateFormData"
          :rules="AddCateFormRules"
          ref="AddCateFormFormRef"
          label-width="100px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="AddCateFormData.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              clearable
              change-on-select
              v-model="CascaderValueArray"
              :options="CascaderData"
              :props="CascaderProp"
              @change="handleCascaderChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="AddCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAddCateRequest">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="修改商品" :visible.sync="EditCateDialogVisible" width="50%">
        <el-form
          :model="EditCateFormData"
          :rules="EditCateFormRules"
          ref="EditCateFormFormRef"
          label-width="100px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="EditCateFormData.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditCateRequest">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      TableTreeData: [],
      TableTreeColumns: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "400px"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "action"
        }
      ],
      total: 0,
      AddCateDialogVisible: false,
      AddCateFormData: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      AddCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      CascaderData: [],
      CascaderProp: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      CascaderValueArray: [],
      EditCateDialogVisible: false,
      EditCateFormData: {},
      EditCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getTableTreeData();
  },
  methods: {
    async getTableTreeData() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryinfo
      });

      this.TableTreeData = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(pagesize) {
      this.queryinfo.pagesize = pagesize;
      this.getTableTreeData();
    },
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.getTableTreeData();
    },
    async handleAddCateDialogVisible() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });

      if (res.meta.status !== 200) return this.$message.error("获取数据失败");
      this.CascaderData = res.data;
      this.AddCateDialogVisible = true;
    },
    handleCascaderChange() {
      if (this.CascaderValueArray.length > 0) {
        this.AddCateFormData.cat_pid = this.CascaderValueArray[
          this.CascaderValueArray.length - 1
        ];
        this.AddCateFormData.cat_level = this.CascaderValueArray.length;
        console.log(this.AddCateFormData);
        return;
      }
      this.AddCateFormData.cat_pid = 0;
      this.AddCateFormData.cat_level = 0;
      console.log(this.AddCateFormData);
    },
    handleAddCateDialogClose() {
      this.$refs.AddCateFormFormRef.resetFields();
      this.CascaderValueArray = [];
      this.AddCateFormData.cat_pid = 0;
      this.AddCateFormData.cat_level = 0;
    },
    handleAddCateRequest() {
      this.$refs.AddCateFormFormRef.validate(async value => {
        if (!value) return;
        const { data: res } = await this.$http.post(
          "categories",
          this.AddCateFormData
        );
        if (res.meta.status !== 201) return this.$message.error("操作失败");
        this.getTableTreeData();
        this.$message.success("添加成功");
        this.AddCateDialogVisible = false;
      });
    },
    async handleEditCateDialogVisible(cat_id) {
      const { data: res } = await this.$http.get(`categories/${cat_id}`);
      this.EditCateFormData = res.data;
      this.EditCateDialogVisible = true;
    },
    handleEditCateRequest() {
      this.$refs.EditCateFormFormRef.validate(async value => {
        if (!value) return;
        const { data: res } = await this.$http.put(
          `categories/${this.EditCateFormData.cat_id}`,
          {
            cat_name: this.EditCateFormData.cat_name
          }
        );
        if (res.meta.status !== 200) return this.$message.error("操作失败");
        this.getTableTreeData();
        this.$message.success("修改成功");
        this.EditCateDialogVisible = false;
      });
    },
    async handleRemoveCate(cat_id) {
      try {
        const confirm = await this.$confirm("确认删除该分类吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        });
        if (confirm === "confirm") {
          const { data: res } = await this.$http.delete(`categories/${cat_id}`);
          if (res.meta.status !== 200) return this.$message.error("操作失败");
          this.getTableTreeData();
          this.$message.success("删除成功");
        }
      } catch (error) {
        this.$message.info("已取消删除");
      }
    }
  }
};
</script>

<style lang="less" scoped>
.TableTree {
  margin: 15px 0;
}
.el-cascader {
  width: 100%;
}
</style>